package com.bzy.blp.upms.service.impl;

import com.bzy.blp.model.TSystemResourceInfoDat;
import com.bzy.blp.upms.mapper.ResourceInfoMapper;
import com.bzy.blp.upms.service.ResourceInfoService;
import com.bzy.blp.upms.vo.ResourceInfoVO;
import com.bzy.blp.upms.vo.RoleResourceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ResourceInfoServiceImpl implements ResourceInfoService {

    @Autowired
    ResourceInfoMapper resourceInfoMapper;


    @Override
    public List<ResourceInfoVO> findResourceByUserIdTree(String user_id, String app_id) throws Exception {
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("userId", user_id);
        map.put("appId", app_id);
        List<ResourceInfoVO> resourceInfoVOTree  = treeResourceInfoVO(resourceInfoMapper.queryResourceByUserId(map));
        return resourceInfoVOTree;
    }

    @Override
    public List<RoleResourceVO> findResourceByRoleId(Long roleId) throws Exception {


        return null;
    }


    private List<ResourceInfoVO> treeResourceInfoVO(List<TSystemResourceInfoDat> dataList) throws Exception {
        List<ResourceInfoVO> resourceInfoVOTree = new ArrayList<ResourceInfoVO>();
        List<ResourceInfoVO> resourceInfoVOList = new ArrayList<ResourceInfoVO>();
        for(TSystemResourceInfoDat node : dataList){
            resourceInfoVOList.add(convert(node));
        }
        for(ResourceInfoVO root : resourceInfoVOList){
            Boolean isRoot = true;
            for(ResourceInfoVO node : resourceInfoVOList){
                if(root.getParent_id().equals(node.getResource_id())){
                    isRoot = false;
                    break;
                }
            }
            if(isRoot){
                resourceInfoVOTree.add(root);
            }
            for (ResourceInfoVO node : resourceInfoVOList) {
                if (node.getParent_id().equals(root.getResource_id())) {
                    if (root.getChildren() == null) {
                        List<ResourceInfoVO> myChildrens = new ArrayList<ResourceInfoVO>();
                        myChildrens.add(node);
                        root.setChildren(myChildrens);
                    } else {
                        root.getChildren().add(node);
                    }
                }
            }
        }
        return resourceInfoVOTree;
    }

    private ResourceInfoVO convert(TSystemResourceInfoDat src){
        ResourceInfoVO dest = new ResourceInfoVO();
        dest.setCreate_time(src.getCreateTime());
        dest.setDescription(src.getDescription());
        dest.setIs_delete(src.getIsDelete()?1:0);
        dest.setParent_id(src.getParentId());
        dest.setParent_path(src.getParentPath());
        dest.setPermissions_code(src.getPermissionsCode());
        dest.setPriority(src.getPriority());
        dest.setResource_id(src.getResourceId());
        dest.setResource_image_uri(src.getResourceImageUri());
        dest.setResource_name(src.getResourceName());
        dest.setResource_request_uri(src.getResourceRequestUri());
        dest.setResource_type(src.getResourceType());
        dest.setUpdate_time(src.getUpdateTime());
        dest.setId(src.getResourceId());
        dest.setText(src.getResourceName());
        return dest;
    }
}
